package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.CommentService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    // 保存
    @Override
    public Integer saveComment(Comment comment) {
        // 1.保存comment对象
        mongoTemplate.save(comment);
        // 2.根据操作类型不同，添加对应数量
        Integer resultCount = 0;
        if (comment.getCommentType() == 1) { // 点赞
            // 查询movmenet对象
            Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
            // 数量+1
            movement.setLikeCount(movement.getLikeCount() + 1);
            mongoTemplate.save(movement);
            // 返回数量
            resultCount = movement.getLikeCount();

        } else if (comment.getCommentType() == 2) { // 评论
            // 查询movmenet对象
            Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
            // 数量+1
            movement.setCommentCount(movement.getCommentCount() + 1);
            mongoTemplate.save(movement);
            // 返回数量
            resultCount = movement.getCommentCount();

        } else if (comment.getCommentType() == 3) {// 喜欢
            // 查询movmenet对象
            Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
            // 数量+1
            movement.setLoveCount(movement.getLoveCount() + 1);
            mongoTemplate.save(movement);
            // 返回数量
            resultCount = movement.getLoveCount();

        }
        return resultCount;
    }

    // 取消
    @Override
    public Integer removeComment(Long userId, Long publishUserId, Integer commentType, String publishId) {
        // 1.根据三个条件删除comment对象
        Query query = new Query(
                Criteria.where("userId").is(userId)  // 操作人（当前登录用户）
                        .and("publishId").is(new ObjectId(publishId)) // 动态id
                        .and("commentType").is(commentType) // 操作类型
        );
        mongoTemplate.remove(query, Comment.class);
        // 2.根据操作类型不同，添加对应数量
        Integer resultCount = 0;
        if (commentType == 1) { // 点赞
            // 查询movmenet对象
            Movement movement = mongoTemplate.findById(new ObjectId(publishId), Movement.class);
            // 数量-1
            movement.setLikeCount(movement.getLikeCount() - 1);
            mongoTemplate.save(movement);
            // 返回数量
            resultCount = movement.getLikeCount();

        } else if (commentType == 2) { // 评论
            // 查询movmenet对象
            Movement movement = mongoTemplate.findById(new ObjectId(publishId), Movement.class);
            // 数量-1
            movement.setCommentCount(movement.getCommentCount() - 1);
            mongoTemplate.save(movement);
            // 返回数量
            resultCount = movement.getCommentCount();

        } else if (commentType == 3) {// 喜欢
            // 查询movmenet对象
            Movement movement = mongoTemplate.findById(new ObjectId(publishId), Movement.class);
            // 数量-1
            movement.setLoveCount(movement.getLoveCount() - 1);
            mongoTemplate.save(movement);
            // 返回数量
            resultCount = movement.getLoveCount();

        }
        return resultCount;
    }

    // 分页查询
    @Override
    public PageBeanVo findCommentByPage(String publishId, Integer pageNum, Integer pageSize, Integer commentType) {
        // 1.构建查询条件
        Integer index = (pageNum - 1) * pageSize;
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(publishId)).and("commentType").is(commentType)) // 条件
                .with(Sort.by(Sort.Order.desc("created"))) // 排序
                .skip(index).limit(pageSize); // 分页
        // 2.查询评论列表
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        // 3.查询总记录数
        long count = mongoTemplate.count(query, Comment.class);
        // 4.返回并封装分页对象
        return new PageBeanVo(pageNum, pageSize, count, commentList);
    }

    // 查询对你点赞、评论、喜欢的人信息
    @Override
    public PageBeanVo findUserCommentByPage(Long publishUserId, Integer pageNum, Integer pageSize, Integer commentType) {
        // 1.构建查询条件
        Query query = new Query(Criteria.where("publishUserId").is(publishUserId).and("commentType").is(commentType))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);

        // 2.查询list集合
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        // 3.查询总记录数
        long count = mongoTemplate.count(query, Comment.class);

        // 4.返回并封装分页对象
        return new PageBeanVo(pageNum, pageSize, count, commentList);
    }

}
